<!-- 盘亏盘盈管理 -->
<template>
  <div class="mod-config">
    <el-button-group style="margin-bottom: 10px;">
      <el-button type="primary" icon="el-icon-search" @click="searchHandle">查询</el-button>
      <el-button type="primary" icon="el-icon-delete" @click="searchReset">清空</el-button>
      <el-button v-if="permissions.eims_check_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle('')">新增</el-button>
    </el-button-group>
    <el-form inline :model="dataForm" label-width="100px" @keyup.enter.native="searchHandle">
      <el-form-item label="单据编号">
        <el-input v-model="dataForm.billId"></el-input>
      </el-form-item>
      <el-form-item label="单据类型">
        <el-select v-model="dataForm.checkType">
          <el-option v-for="item in checkTypeOption" :value="item.value" :label="item.label" :key="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态">
        <el-select v-model="dataForm.checkStatus">
          <el-option v-for="item in checkStatusOption" :value="item.value" :label="item.label" :key="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="仓位">
        <el-select v-model="dataForm.stockId">
          <el-option v-for="item in stockOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="制单人">
        <el-input v-model="dataForm.username"></el-input>
      </el-form-item>
      <el-form-item label="审核人">
        <el-input v-model="dataForm.auditName"></el-input>
      </el-form-item>
      <el-form-item label="盘点类型">
        <el-select v-model="dataForm.ctgId">
          <el-option v-for="item in ctgOption" :value="item.id" :label="item.name" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单据日期">
        <el-row>
          <el-col :span="11">
            <el-date-picker v-model="dataForm.createTimeStart" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
          </el-col>
          <el-col class="line" :span="2" style="text-align: center;">-</el-col>
          <el-col :span="11">
            <el-date-picker v-model="dataForm.createTimeEnd" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    <div class="avue-crud">
      <el-table :data="dataList" border v-loading="dataListLoading" :max-height="tableHeight">
        <el-table-column prop="billId" label="单据编号" width="180"></el-table-column>
        <el-table-column prop="ctgId" label="盘点类型" width="80">
          <template slot-scope="scope"> {{getCtgName(scope.row.ctgId)}} </template>
        </el-table-column>
        <el-table-column prop="checkType" label="单据类型" width="80">
          <template slot-scope="scope">
            <span v-if="scope.row.checkType == '1'">盘盈</span><span v-else>盘亏</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="单据日期" width="155"></el-table-column>
        <el-table-column prop="stockName" label="仓位"></el-table-column>
        <el-table-column prop="auditTime" label="审核日期" width="155"></el-table-column>
        <el-table-column prop="checkStatus" label="审核状态" width="80">
          <template slot-scope="scope">
            <span v-if="scope.row.checkStatus == '1'">已审核</span><span v-else>未审核</span>
          </template>
        </el-table-column>
        <el-table-column prop="username" label="制单人" width="65"></el-table-column>
        <el-table-column prop="auditName" label="审核人" width="65"></el-table-column>
        <el-table-column label="操作" width="210" fixed="right">
          <template slot-scope="scope">
            <div v-if="scope.row.checkStatus == '1'">
              <el-button type="text" icon="el-icon-s-opportunity" @click="getInfo(scope.row.id)">详情</el-button>
              <el-button v-if="permissions.eims_check_edit" type="text" icon="el-icon-s-opportunity"
                disabled="disabled">审核</el-button>
              <el-button v-if="permissions.eims_check_del" type="text" icon="el-icon-delete" disabled="disabled">删除</el-button>
            </div>
            <div v-else>
              <el-button type="text" icon="el-icon-s-opportunity" @click="getInfo(scope.row.id)">详情</el-button>
              <el-button v-if="permissions.eims_check_edit && userInfo.lessDeptId == 1" type="text" icon="el-icon-s-opportunity"
                @click="audit(scope.row.id)">审核</el-button>
              <el-button v-if="permissions.eims_check_edit" type="text" icon="el-icon-delete" @click="editCheckInfo(scope.row.id)">修改</el-button>
              <el-button v-if="permissions.eims_check_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="avue-crud__pagination">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100, 200]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
    <!-- 弹窗, 新增 -->
    <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
    <!-- 弹窗, 修改 -->
    <edit-form v-if="editVisible" ref="editCheck" @refreshDataList="getDataList"></edit-form>
    <!-- 弹窗, 详情 -->
    <info-form v-if="infoVisible" ref="info" @refreshDataList="getDataList"></info-form>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { checkFetchList, delCheckOrder, auditById } from '@/api/eims/check'
  import { getCtgList } from '@/api/eims/ctg'
  import { getStockList, getStockInfo } from '@/api/eims/stock'

  import TableForm from './form'
  import EditForm from './editForm'
  import InfoForm from './info'

  export default {
    data() {
      return {
        dataForm: {},
        ctgOption: [],
        stockOptions: [],
        checkTypeOption: [{label: '盘盈',value: '1'},{label: '盘亏',value: '2'}],
        checkStatusOption: [{label: '已审核',value: '1'},{label: '未审核',value: '0'}],
        dataList: [],
        pageIndex: 1,
        pageSize: 200,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        infoVisible: false,
        editVisible: false
      }
    },
    components: {
      TableForm,
      InfoForm,
      EditForm
    },
    created() {
      this.getCtgList()
      this.stockList()
    },
    computed: {
      ...mapGetters(['permissions', 'userInfo']),
      tableHeight: function(){
        return window.innerHeight - 260
      }
    },
    methods: {
      //  查询数据
      searchHandle() {
        this.pageIndex = 1
        this.getDataList()
      },
      //  清空搜索表单
      searchReset() {
        for (let key in this.dataForm) {
          this.dataForm[key] = ''
        }
      },
      // 获取数据列表
      getDataList() {
        let params1 = {
          current: this.pageIndex,
          size: this.pageSize
        }
        let params2 = this.filterParams(this.dataForm)
        this.dataListLoading = true
        checkFetchList(Object.assign(params1, params2)).then(response => {
					if(response.data.code == '0'){
						this.dataList = response.data.data.records
						this.totalPage = response.data.data.total
					}else{
						this.$message.error(response.data.msg)
					}
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 修改
      editCheckInfo(id) {
        this.editVisible = true
        this.$nextTick(() => {
          this.$refs.editCheck.init(id)
        })
      },
      //查看详情
      getInfo(id) {
        this.infoVisible = true
        this.$nextTick(() => {
          this.$refs.info.init(id)
        })
      },
      //审核按钮
      audit(id) {
        this.$confirm('是否确认审核ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return auditById(id)
        }).then(response => {
					if(response.data.code == '0'){
						this.$message.success('审核成功')
						this.getDataList()
					}else{
						this.$message.error(response.data.msg)
					}
        })
      },
      // 删除
      deleteHandle(id) {
        this.$confirm('是否确认删除ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delCheckOrder(id)
        }).then(response => {
					if(response.data.code == '0'){
						this.$message.success('删除成功')
						this.getDataList()
					}else{
						this.$message.error(response.data.msg)
					}
        })
      },
      //  获取商品类型列表
      getCtgList() {
        getCtgList().then(response => {
					if(response.data.code == '0'){
						this.ctgOption = response.data.data
					}else{
						this.$message.error(response.data.msg)
					}
        })
      },
      //  获取商品名字
      getCtgName(value) {
        let obj = {};
        obj = this.ctgOption.find((item) => {
          return item.id == value;
        })
        if (obj) {
          return obj.name
        }
      },
      //  获取仓位列表
      stockList() {
        if (this.userInfo.lessDeptId == '1') {
          getStockList().then(response => {
						if(response.data.code == '0'){
							this.stockOptions = response.data.data
							this.getDataList()
						}else{
							this.$message.error(response.data.msg)
						}
          })
        } else {
          getStockInfo(this.userInfo.lessDeptId).then(response => {
            if(response.data.code == '0'){
            	this.stockOptions = response.data.data
            	if (this.stockOptions.length > 0) {
            	  this.dataForm.stockId = this.stockOptions[0].id
            	  this.getDataList()
            	}
            }else{
            	this.$message.error(response.data.msg)
            }
          })
        }
      }
    }
  }
</script>
